package com.nj.dms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.PageBean;
import com.nj.dms.dao.UserDao;
import com.nj.dms.dao.impl.UserDaoImpl;

/**
 * 用户信息查询
 */
@WebServlet("/UserList")
public class UserList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 设置每页显示的最大记录数
	private int maxSize = 13;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// curPage表示要显示的页[当前页],默认值是1
		int curPage = 1;
		// 获取页面传递过来的要显示的当前页的值
		String scurPage = request.getParameter("curPage");
		if (scurPage != null) {
			curPage = Integer.parseInt(scurPage.trim());
		}
		// 计算显示记录的开始位置
		Integer beginIndex = (curPage - 1) * maxSize;
		// dao data access object
		UserDao userDao = new UserDaoImpl();
		// 从数据库获取页面显示数据的列表
		List userlist = userDao.findUserByPage(beginIndex, maxSize);
		request.setAttribute("userlist", userlist);// 将listUser集合设置到request属性中
		// 分页信息设置
		// 从数据库获取查询总的记录数
		int recordNums = userDao.countUsers();
		// 根据当前页 每页显示的最大记录数 总的记录数 来计算上页 下页 尾页 总页数
		PageBean pageBean = new PageBean();
		// 调用PageBean类的init()方法完成分页信息的设置
		pageBean.init(curPage, maxSize, recordNums);
		// 将当前分页信息对象pageBean保存在request属性范围
		request.setAttribute("pageBean", pageBean);

		// 通过request.getRequestDispatcher("admin/userlist.jsp").forward(request,response)方法
		// 实现用户数据集合listUser中的用户信息在userlist.jsp文件的显示

		request.getRequestDispatcher("user/user-list.jsp").forward(request, response);

	}

}
